package com.cloudexpress.action;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import com.cloudexpress.common.Utils;
import com.cloudexpress.persistence.model.User;
import com.cloudexpress.service.UserService;

public class LoginAction extends BaseActionSupport {
	
	@Autowired
	private UserService userService;
	
	private Logger logger = Logger.getLogger(this.getClass());
	
	/**
	 * 登录页面
	 * @return
	 * @throws Exception
	 */
	public String login() throws Exception {
		
		return INPUT;
	}
	
	public String loginCheck() throws Exception {
		
		List<User> userList = userService.getUserByEmail(getUserEmail());
		boolean isSuccess = false;
		if(!userList.isEmpty()) {
			User user = userList.get(0);
			if(user.getPassword().equals(Utils.encryptPassword(getPassword()))) {
				isSuccess = true;
			}
		} 
		
		if(!isSuccess) {
			addActionError(getText("error.login"));
			return INPUT;
		}
		
		return SUCCESS;
	}
	
	private String userEmail;
	
	private String password;

	public String getUserEmail() {
		return userEmail;
	}

	public void setUserEmail(String userEmail) {
		this.userEmail = userEmail;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}
	
	

}
